<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="选择店铺" prop="sellerId">
        <el-select v-model="queryParams.sellerId" placeholder="请选择店铺" filterable clearable>
          <el-option v-for="(item,index) in storeList" :key="index"
                     :label="item.storeUserName + ' - ' + item.storeName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input v-model="queryParams.productId" placeholder="请输入产品ID" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getProductDetail">查 询</el-button>
      </el-form-item>
    </el-form>

    <!-- 添加或修改店铺cookie信息保存对话框 -->
    <el-dialog title="上传白底图" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品主图" prop="imageURLs">
          <div class="flex-wrap ml5 mr5 mt5 mb5" style="display: inline-flex;flex-direction: column;"
               v-for="(item,index) in form.imageURLs"
               :key="index">
            <image-preview :src="item" width="100px"></image-preview>
          </div>
        </el-form-item>
        <el-form-item label="白底图" prop="whiteImg">
          <image-upload v-model="form.whiteImg" :limit="1" atlasType="3" isShowSize isShowRecommend
                        isLimitsize></image-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryAeProduct,
  editProductByAeData,
} from "@/api/picture/optimizeTask";
import {getStoreInfo} from "@/api/order/orderApi";

export default {
  name: "SellerStoreCookie",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // sellerId: '1722069851071942657',
        // productId: '1005005551988595',
        sellerId: undefined,
        productId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        whiteImg: [{required: true, message: "请上传白底图", trigger: "change"}],
      },
      storeList: [],//店铺列表
    };
  },
  created() {
    this.getStoreList();
  },
  methods: {
    getProductDetail() {
      this.reset()
      queryAeProduct(this.queryParams).then(res => {
        console.log('###', JSON.parse(res.data))
        this.open = true
        let data = JSON.parse(res.data).aliexpress_offer_product_query_response.result
        console.log('res', data)
        this.form.imageURLs = data.image_u_r_ls.split(';')
      })
    },
    //  获取店铺列表
    getStoreList() {
      this.loading = true
      getStoreInfo().then(response => {
        this.storeList = response
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        imageURLs: undefined,
        whiteImg: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          let data = {
            aeProductId: this.queryParams.productId,
            storeId: this.queryParams.sellerId,
            type: 3,
            whiteImg: this.form.whiteImg
          }
          editProductByAeData(data).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
  }
};
</script>
